<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 *订单管理
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
$tabs=array(
    array('op'=>'list','title'=>'全部开奖记录'),
);
if($_RQ['op']=='list'){
    $return['params']=array();
    $params=array('uniacid'=>$_SESSION['uniacid']);
    if(isset($_RQ['status']) && $_RQ['status']!=100){
        $params['status']=intval($_RQ['status']);
        $return['params']['status']=intval($_RQ['status']);
    }
    if(isset($_RQ['mid'])){
        $params['mid']=intval($_RQ['mid']);
        $return['params']['mid']=intval($_RQ['mid']);
    }
    $pindex = max(1, intval($_RQ['page']));
    $psize = 10;
    $list=pdo_getslice('jy_manghe_openlog',$params,array($pindex,$psize),$total,$fields,'','createtime desc');
    if($list){
        foreach($list as $k=>&$item){
            $item['title']=pdo_getcolumn('jy_weishop_game',array('id'=>$item['gameid'],'uniacid'=>$_SESSION['uniacid']),'title');
            $day=date('Y-m-d',$item['opentime']);
            $item['dayno']=$day.'第'.$item['dayno'].'期';
            $item['isfee']=pdo_getsum('jy_manghe_feelog',array('openlogid'=>$item['id'],'uniacid'=>$_SESSION['uniacid']),'fee');
            $item['isfee']=$item['isfee']?:0;
            $members=pdo_getall('jy_manghe_feelog',array('openlogid'=>$item['id'],'uniacid'=>$_SESSION['uniacid']),'mid','mid');
            if($members){
                $members=array_keys($members);
                $item['membernum']=count($members);
            }else{
                $item['membernum']=0;
            }
        }
    }
    $return['list']=$list;
    $totalpage=ceil($total/$psize);
    $totalpage=intval($totalpage);
    $return['totalpage'] =$totalpage;
    $return['params']['page'] =$pindex;
    $htmls=array(
        'type'=>'list',
        'tabs'=>$tabs,
        'list'=>array('data'=>'list','edit'=>false,'del_one'=>false,'delall'=>false,'list'=>array(
            array('name'=>'id','title'=>'开奖期ID','type'=>'string'),
            array('name'=>'title','title'=>'活动名称','type'=>'string'),
            array('name'=>'dayno','title'=>'每日期数','type'=>'string'),
            array('type'=>'img_string','title'=>'参与积分/人数',
                'data'=>array(
                    array('title'=>'积分数量：','name'=>'isfee'),
                    array('title'=>'总人数：','name'=>'membernum')
                )
            ),
            array('name'=>'status','title'=>'状态','type'=>'radio','options'=>'statuss'),
            array('name'=>'opentype','title'=>'获奖类型','type'=>'radio','options'=>'opentype'),
            array('name'=>'opentime','title'=>'开奖时间','type'=>'time'),
        ),'action'=>array(
            array('type'=>'href','op'=>'setopen','title'=>'指定开奖','show'=>array('status'=>0)),
            array('type'=>'modal','op'=>'look','title'=>'查看开奖结果','show'=>array('status'=>1)),
            array('type'=>'modal','op'=>'lookset','title'=>'查看指定','show'=>array('status'=>1)),
        )),
        'search'=>array(
            array('type'=>'radio','width'=>15,'name'=>'status','options'=>'statuss','title'=>'状态'),
            array('type'=>'choose','width'=>15,'action'=>'core/member.get_member/','name'=>'mid','title'=>'选择会员'),
        ),
    );
    $return['statuss']=$this->log_status;
    $return['opentype']=$this->he_opentype;
    $return['htmls']=$htmls;
    exi($return);
}
if($_RQ['op']=='look' || $_RQ['op']=='lookset'){
    $item=$this->get('openlog',array('id'=>intval($_RQ['id'])));
    if(!$item){
        exi('指定记录不存在！','error');
    }
    $reward=pdo_getcolumn('jy_weishop_game',array('id'=>$item['gameid']),'reward');
    $reward=json_decode($reward,true);
    if($_RQ['op']!='lookset'){
        $item['reward']=json_decode($item['reward'],true);
        paixu($item['reward'],'rewardkey','asc');
        foreach($item['reward'] as &$rv){
            $rv['isfee']=pdo_getsum('jy_manghe_feelog',array('rewardkey'=>$rv['rewardkey'],'openlogid'=>$item['id'],'uniacid'=>$_SESSION['uniacid']),'fee');
            $rv['isfee']=$rv['isfee']?:0;
            $num=$rv['rewardkey']+1;
            $rv['title']=$num;
            $rv['newkeytitle']=$reward[$rv['newkey']]['title'];
        }
    }else{
        $item['setreward']=json_decode($item['setreward'],true);
        foreach($item['setreward'] as $k=>&$av){
            $av['isjifen']=pdo_getsum('jy_manghe_feelog',array('rewardkey'=>$k,'openlogid'=>$item['id'],'uniacid'=>$_SESSION['uniacid']),'fee');
            $av['nodel']=1;
            $num=$k+1;
            $av['title']=$num;
            $av['newkeytitle']=$reward[$av['newkey']]['title'];
        }
    }
    $return['item']=$item;
    if($_RQ['op']!='lookset'){
        $return['htmls']=array(
            'type'=>'look',
            'desc'=>'开奖信息',
            'look'=>array(
                array(
                    'type'=>'array',
                    'name'=>'reward',
                    'title'=>'开奖信息',
                    'list'=>array(
                        array('type'=>'string','title'=>'前端显示盲盒号','name'=>'title'),
                        array('type'=>'string','title'=>'已投入数量','name'=>'isfee'),
                        array('type'=>'string','title'=>'实际指定开盲盒','name'=>'newkeytitle'),
                        array('type'=>'string','title'=>'奖励额度','name'=>'jiangfee'),
                    ),
                ),
            ),
        );
    }else{
        $return['htmls']=array(
            'type'=>'look',
            'desc'=>'指定信息',
            'look'=>array(
                array(
                    'type'=>'array',
                    'name'=>'setreward',
                    'title'=>'指定信息',
                    'list'=>array(
                        array('type'=>'string','title'=>'前端显示盲盒号','name'=>'title'),
                        array('type'=>'string','title'=>'已投入数量','name'=>'isjifen'),
                        array('type'=>'string','title'=>'实际指定开盲盒','name'=>'newkeytitle'),
                    ),
                ),
            ),
        );
    }
    exi($return);
}
if($_RQ['op']=='setopen'){
    if($_RQ['postdata']){
        $post=get_postdata();
        $id=intval($post['id']);
        $update=array(
            'setreward'=>ijson_encode($post['setreward']),
        );
        $this->pdo_update('openlog',$update,array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
        exi('设置成功！','','//list/');
    }
    $id=intval($_RQ['id']);
    $item=$this->pdo_get('openlog',array('id'=>$id));
    $rest_time=$item['opentime']-TIMESTAMP;
    if($rest_time>0){
        $item['rest_time']=model('jy_weishop/model')->date($rest_time);
    }
    $reward=pdo_getcolumn('jy_weishop_game',array('id'=>$item['gameid']),'reward');
    $reward=$reward?json_decode($reward,true):array();
    if(!$item['setreward']){
        $item['setreward']=$reward;
    }else{
        $item['setreward']=json_decode($item['setreward'],true);
    }
    foreach($reward as $k=>&$rv){
        $rv['id']=$k;
    }
    foreach($item['setreward'] as $k=>&$av){
        $av['rewardkey']=$k;
        $av['isjifen']=pdo_getsum('jy_manghe_feelog',array('rewardkey'=>$k,'openlogid'=>$item['id'],'uniacid'=>$_SESSION['uniacid']),'fee');
        $av['nodel']=1;
        $num=$k+1;
        $av['title']=$num;
    }
    $return['item']=$item;
    $htmls=array(
        'type'=>'edit',
        'desc'=>'指定开奖',
    );
    $opentime=date('Y-m-d H:i:s',$item['opentime']);
    $htmls['edit']=array(
        array('type'=>'tip','title'=>'指定后，当前盲盒会固定开奖！本轮开奖时间：'.$opentime),
        array('type'=>'rest_time','title'=>'开奖倒计时：','name'=>'rest_time'),
        array(
            'type'=>'array',
            'name'=>'setreward',
            'title'=>'指定开奖',
            'noadd'=>1,'nosplice'=>1,
            'list'=>array(
                array('type'=>'string','title'=>'前端显示盲盒号','name'=>'title'),
                array('type'=>'string','title'=>'已投入数量','name'=>'isjifen'),
                array('type'=>'select','title'=>'实际指定开盲盒','name'=>'newkey','options'=>'reward'),
            ),
            'details'=>array(
                //array('type'=>'copy','title'=>'盲盒号','name'=>'title'),
                //array('type'=>'copy','title'=>'已投入数量','name'=>'isjifen'),
                array('type'=>'select','title'=>'实际指定开盲盒','name'=>'newkey','options'=>'reward'),
            )
        ),
    );
    $return['htmls']=$htmls;
    $return['reward']=$reward;
    exi($return);
}
